package com.mall.commons.result.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @program: Test
 * @description: 类型转换工具类
 * @author: shit_code_builder
 * @create: 2021-06-13 16:30
 **/
public class ObjectTransfer {


    /**
     *
     * @param e 被转换目标对象
     * @param eClass 被转化目标对象类
     * @param tClass 转化对象类
     * @return  转化完成对象
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
  public static<E,T> T change(E e, Class<E> eClass, Class<T> tClass) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
    return change(e,eClass,tClass,null);
  }


    /**
     *
     * @param e 被转换目标对象
     * @param eClass 被转化目标对象类
     * @param tClass 转化对象类
     * @param mapping 目标对象与被转化对象需要转化的属性名关系映射
     * @return 转化完成目标
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static<E,T> T change(E e, Class<E> eClass, Class<T> tClass,Map<String,String> mapping) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        T t = tClass.newInstance();
        Method[] methods = tClass.getMethods();
        if (mapping!=null){
            for (Method method : methods) {
                String name = method.getName();
                if (name.contains("set")){
                    String fieldName = name.replace("set", "");
                    char[] charsDown = fieldName.toCharArray();
                    charsDown[0]+=32;
                    String k = new String(charsDown);
                    String s = mapping.get(k);
                    char[] charsUp ;
                    if (s!=null){
                        charsUp=s.toCharArray();
                    }else {
                       charsUp = k.toCharArray();

                    }
                    charsUp[0]-=32;
                    String methodName="get"+new String(charsUp);
                    try {
                        Object invoke = eClass.getMethod(methodName).invoke(e);
                        method.invoke(t,invoke);
                    }catch (Exception ex){
                        System.out.println("无"+methodName+"方法");
                    } } } }
        else{
            for (Method method : methods) {
                String name = method.getName();
                if (name.contains("set")){
                    String replace = name.replace("set", "get");
                    try {
                        Method methodE = eClass.getMethod(replace);
                        Object invoke = methodE.invoke(e);
                        method.invoke(t,invoke);
                    }catch (Exception ex){
                        System.out.println("无"+replace+"方法");
                    } } } }
        return t;

    }

}
